﻿#include "configoperate.h"

ConfigOperate::ConfigOperate()
{
    m_cfgFile = "user.ini";
    m_port = "55555";

    m_hostName = "localhost";
    m_databaseName = "hotelmanage";
    m_userName = "root";
    m_password = "123123";

    m_houseImagePath = "F:/ForWork/HotelPlatform/image/house";
    m_hotelImagePath = "F:/ForWork/HotelPlatform/image/hotel";
    m_commentImagePath = "F:/ForWork/HotelPlatform/image/comment";
    m_licenseImagePath = "F:/ForWork/HotelPlatform/image/businessLicense";
    m_headImagePath = "F:/ForWork/HotelPlatform/image/head";
    m_certImagePath = "F:/ForWork/HotelPlatform/image/certID";
    initSystemInfo();
}

void ConfigOperate::initSystemInfo()
{
    QFileInfo info(m_cfgFile);
    if (!info.isFile()) /// 该文件不存在，创建文件-写入默认的配置
    {
        writeSystemInfo();
    }
    else {                  /// 该文件存在，创建文件-写入用户自定义的配置
        readSystemInfo();
    }
}

void ConfigOperate::readSystemInfo()
{
    QStringList valueList;
    QStringList keyList;
    valueList.clear();
    keyList.clear();

    keyList.append("HostName");
    keyList.append("DatabaseName");
    keyList.append("UserName");
    keyList.append("Password");
    readInit("DataBaseConnectAttribute",keyList,valueList);

    while (valueList.length() < 4)
    {
        valueList.append(" ");
    }
    m_hostName = valueList.at(0);
    m_databaseName  = valueList.at(1);
    m_userName = valueList.at(2);
    m_password = valueList.at(3);

    readInit("TCPServerPort", "port", m_port);
    readInit("HouseImagePath", "housePath", m_houseImagePath);
    readInit("CommentImagePath", "commentPath", m_commentImagePath);
    readInit("LicenseImagePath", "licensePath", m_licenseImagePath);
    readInit("HotelImagePath", "hotelPath", m_hotelImagePath);
    readInit("CertImagePath", "certPath", m_certImagePath);
    readInit("HeadImagePath", "headPath", m_headImagePath);
}
void ConfigOperate::writeSystemInfo()
{
    QSettings config(m_cfgFile, QSettings::IniFormat);

    config.beginGroup("DataBaseConnectAttribute");
    config.setValue("HostName", m_hostName);
    config.setValue("DatabaseName", m_databaseName);
    config.setValue("UserName", m_userName);
    config.setValue("Password", m_password);
    config.endGroup();

    config.beginGroup("TCPServerPort");
    config.setValue("port", m_port);
    config.endGroup();

    config.beginGroup("CommentImagePath");
    config.setValue("commentPath", m_commentImagePath);
    config.endGroup();

    config.beginGroup("HouseImagePath");
    config.setValue("housePath", m_houseImagePath);
    config.endGroup();

    config.beginGroup("LicenseImagePath");
    config.setValue("licensePath", m_licenseImagePath);
    config.endGroup();

    config.beginGroup("HotelImagePath");
    config.setValue("hotelPath", m_hotelImagePath);
    config.endGroup();

    config.beginGroup("CertImagePath");
    config.setValue("certPath", m_certImagePath);
    config.endGroup();

    config.beginGroup("HeadImagePath");
    config.setValue("headPath", m_headImagePath);
    config.endGroup();
}

bool ConfigOperate::writeInit(QString group, QString key, QString value)
{
    if(group.isEmpty() || key.isEmpty())
    {
        return false;
    }else
    {
        //创建配置文件操作对象
        QSettings config(m_cfgFile, QSettings::IniFormat);

        //将信息写入配置文件
        config.beginGroup(group);
        config.setValue(key, value);
        config.endGroup();

        return true;
    }
}
bool ConfigOperate::readInit(QString group, QString key, QString &value)
{
    value.clear();
    if(group.isEmpty() || key.isEmpty())
    {
        return false;
    }
    else
    {
        //创建配置文件操作对象
        QSettings config(m_cfgFile, QSettings::IniFormat);

        //读取用户配置信息
        value = config.value(group + "/" + key).toString();

        return true;
    }
}
bool ConfigOperate::readInit(QString group, QStringList keyList, QStringList &valueList)
{
    QString value;
    valueList.clear();

    if(group.isEmpty() || keyList.length() < 1)
    {
        return false;
    }
    else
    {
        QSettings config(m_cfgFile, QSettings::IniFormat);//创建配置文件操作对象

        for (int i = 0; i < keyList.length(); i++)
        {
            value = config.value(group + "/" + keyList.at(i)).toString();//读取用户配置信息
            valueList.append(value);
        }

        return true;
    }
}
bool ConfigOperate::writeInit(QString group, QStringList keyList, QStringList valueList)
{
    if(group.isEmpty() || keyList.length() < 1)
    {
        return false;
    }else
    {
        QSettings config(m_cfgFile, QSettings::IniFormat); //创建配置文件操作对象

        config.beginGroup(group);
        for (int i = 0; i < keyList.length(); i++)//将信息写入配置文件
        {
            config.setValue(keyList.at(i), valueList.at(i));
        }
        config.endGroup();

        return true;
    }
}

void ConfigOperate::writeDataBaseAttribute(QStringList& valueList)
{
    QStringList keyList;
    keyList.clear();

    keyList.append("HostName");
    keyList.append("DatabaseName");
    keyList.append("UserName");
    keyList.append("Password");
    writeInit("DataBaseConnectAttribute",keyList,valueList);
}
void ConfigOperate::writeDataBaseAttribute(QString hostName,
                                  QString databaseName,QString userName,QString password)
{
    QStringList valueList;
    valueList.clear();

    valueList.append(hostName);
    valueList.append(databaseName);
    valueList.append(userName);
    valueList.append(password);
    writeDataBaseAttribute(valueList);
}
void ConfigOperate::writePort(QString value)
{
    m_port = value;
    writeInit("TCPServerPort", "port", m_port);
}
void ConfigOperate::writeCommentImagePath(QString value)
{
    m_commentImagePath = value;
    writeInit("CommentImagePath", "commentPath", m_commentImagePath);
}
void ConfigOperate::writeHouseImagePath(QString value)
{
    m_houseImagePath = value;
    writeInit("HouseImagePath", "housePath", m_houseImagePath);
}
void ConfigOperate::writeHotelImagePath(QString value)
{
    m_hotelImagePath = value;
    writeInit("HotelImagePath", "hotelPath", m_hotelImagePath);
}
void ConfigOperate::writeLicenseImagePath(QString value)
{
    m_licenseImagePath = value;
    writeInit("LicenseImagePath", "licensePath", m_licenseImagePath);
}
void ConfigOperate::writeCertImagePath(QString value)
{
    m_certImagePath = value;
    writeInit("CertImagePath", "certPath", m_certImagePath);
}
void ConfigOperate::writeHeadImagePath(QString value)
{
    m_headImagePath = value;
    writeInit("HeadImagePath", "headPath", m_headImagePath);
}

QString ConfigOperate::readCertImagePath()
{
    return m_certImagePath;
}
QString ConfigOperate::readHeadImagePath()
{
    return m_headImagePath;
}
QString ConfigOperate::readCommentImagePath()
{
    return m_commentImagePath;
}
QString ConfigOperate::readHouseImagePath()
{
    return m_houseImagePath;
}
QString ConfigOperate::readHotelImagePath()
{
    return m_hotelImagePath;
}
QString ConfigOperate::readLicenseImagePath()
{
    return m_licenseImagePath;
}

QString ConfigOperate::readPort()
{
    return m_port;
}
QString ConfigOperate::readHostName()
{
    return m_hostName;
}
QString ConfigOperate::readDatabaseName()
{
    return m_databaseName;
}
QString ConfigOperate::readUserName()
{
    return m_userName;
}
QString ConfigOperate::readPassword()
{
    return m_password;
}
QStringList ConfigOperate::readDataBaseAttribute()
{
    QStringList valueList;

    valueList.append(m_hostName);
    valueList.append(m_databaseName);
    valueList.append(m_userName);
    valueList.append(m_password);

    return valueList;
}
